package pdf解析.controller;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pdf解析.entity.MyUser;
import pdf解析.service.AopService;
import pdf解析.service.PdfService;
import 事务练习.entity.TbUser;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author caihe
 * @date 2021/6/3 18:06
 */
@RestController
public class PdfController {

    public static void main(String[] args) {
//        List<String> kouBeiComboCardNameList = Arrays.asList("1","2");
//        List<String> words = Arrays.asList("2", "1");
//
//       
//        System.out.println(3);
        String a = "+123.0";
        System.out.println(a.matches("(\\d+\\.\\d+)|(\\d)"));
        System.out.println(a.matches("(\\d)"));

    }

//    @Autowired
//    private PdfService pdfService;

    @Autowired
    private List<PdfService> pdfServices;

    @Autowired
    private Map<String, PdfService> pdfServiceMap;
    
//    private AopService aopService;
//    
//    @GetMapping("/rpc")
//    public void rpc() {
//        aopService.printTest();
//    }


    @PutMapping("/test")
    public void test(@RequestBody MyUser myUser, HttpServletRequest request) {

        System.out.println(myUser);
        System.out.println("change2 Test");
    }


    @PostMapping("/pdf")
    public TbUser pdfdemo(@RequestParam("file") MultipartFile file, @RequestParam("num") String num) {
        System.out.println(file);

        //checkInsuranceItem(file);
        TbUser t = new TbUser(1, "dema", "xiya");
        throw new RuntimeException();
    }

//    public static void main(String[] args) {
//
//        String a = "";
//        String newStr = "保险费合计（人民币大写）： 贰仟叁佰玖拾叁元玖角伍分 （¥： 2,393.95 元）";
//        String re = "(.*)保险费1(.*)";
//        String[] regs = new String[]{
//                "(.*)保(.*)费合计(.*?)((¥|￥)(：|:)?)(.*)",
//                "保险费人民币(.*)小写(^.{0,1})",
//                "保险费合计(.*?)(RMB(：|:)?(¥|￥)?)(.*)",
//                "(.*)RMB(.*)"};
//        System.out.println(Double.valueOf(String.valueOf("1111.0")));
//        for (String reg :
//                regs) {
//            System.out.println(newStr.matches(reg));
//
//        }
//
//
////        String str = "a合计（人民币大写）： 叁佰元整 （¥: 300.00 元）".replaceAll(" ", ""); 1
////        System.out.println(str.indexOf(a));
////        System.out.println(str.matches("(^.{0,1})合计（人民币大写）：(.*)（¥:"));2
////
////        String ss = "保险费合计（人民币大写）：壹仟壹佰元整（￥:1100.00元）其中救助基金（1.00%）￥：元".replace(" ", ""); 3
////        System.out.println(ss.matches("保险费合计(.*)(RMB|((¥|￥)(：|:)))(.*)"));
////
////        String ssss = "保险费合计: RMB6386.84元（不含税保费:6025.32元，税额:361.52元）".replace(" ", ""); 4
////        System.out.println(ssss.matches("((^.{0,1})合计（人民币大写）：(.*)（¥:)|(保险费合计(.*)RMB(.*))"));
//
//
////        String sss = "112312PDZA奥术大师多".replace("", "");
////        System.out.println(sss.matches("(.*)(PDAA|PDZA)(.*)"));
//
////        String sss = "112312PDZA奥术大师多".replace("", "");
////        System.out.println(sss.matches("(.*)(PDAA|PDZA)(.*)"));
////
//////        String s4 = "　保险费合计(人民币大写):捌佰零柒元伍角(￥:807.50元)其中救助基金(1.50%)￥:12.11元";
//////        System.out.println(s4.replaceAll("　", ""));
////        
////
////
//////        保险费合计（人民币大写）：壹仟壹佰元整（¥：1100.00元）其中救助基金（1.00%）¥：元
//////        保险费合计(.*)(RMB|((¥|￥)(：|:)))(.*)
////        String firstIndex = "保险费合计(.*?)(RMB|((¥|￥)(：|:)?))(.*)";
////        String operatorStr = "保险费合计（人民币大写）壹仟肆佰肆拾捌元玖角柒分 （￥:1,448.97元） ".replaceAll(" ", "");
////        System.out.println(operatorStr.matches(firstIndex));
////        Pattern pattern = Pattern.compile(firstIndex.substring(0, firstIndex.length() - 4));
////        Matcher matcher = pattern.matcher(operatorStr);
////        while (matcher.find()) {
////            // 匹配到关键字后
////            String group = matcher.group();
////            System.out.println(group);
////
////        }
//
////        String reg = "(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))";
////        String carNO = "川A125D";
////        System.out.println(carNO.matches(reg));
//
////        String cc = "车牌底色：蓝车牌号：湘A0GR83发动机号：103808";
////        System.out.println(cc.matches(re));
//
//
//    }


    public static void checkInsuranceItem(MultipartFile file) {

        List<String> insuranceTypes = Arrays.asList("机动车交通事故责任强制保险单", "机动车综合商业保险保险单");
        List<String> trafficItems = Arrays.asList(
                "保险费合计",
                "代收车船税合计",
                "死亡伤残赔偿限额");
        List<String> businessItems = Arrays.asList(
                "保险费合计",
                "机动车第三者责任保险",
                "机动车损失",
                "第三者责任险",
                "机动车车上人员责任保险（司机）",
                "机动车车上人员责任保险（乘客）");


        String s = readPDFText(file);
        // 替换调所以的 逗号
        String ss = s.replaceAll(",", "");
//        System.out.println(ss);
        // 机动车交通事故责任强制保险单
        if (s.contains("机动车交通事故责任强制保险单")) {
            trafficItems.forEach(item -> {
                if (ss.contains(item)) {
                    int startIndex = ss.indexOf(item);
                    int endIndex = startIndex + item.length();
                    System.out.print(ss.substring(startIndex, endIndex) + " ");
                    System.out.println(getMoney(ss.substring(endIndex), item));
                }
            });
            // 机动车商业保险保险单
        } else if (ss.contains("商业保险保险单")) {
            businessItems.forEach(item -> {
                if (ss.contains(item)) {
                    int startIndex = ss.indexOf(item);
                    int endIndex = startIndex + item.length();
                    System.out.print(ss.substring(startIndex, endIndex) + " ");
                    System.out.println(getMoney(ss.substring(endIndex), item));
                }
            });
        }
    }

    public static String readPDFText(MultipartFile file) {
        PDFTextStripper pdfStripper;
        PDDocument pdDoc;
        //File file = new File(filePath);
        try {
            pdfStripper = new PDFTextStripper();
            pdfStripper.setSortByPosition(true);
            pdDoc = PDDocument.load(file.getInputStream());
            return pdfStripper.getText(pdDoc);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getMoney(String str, String item) {
        String regxp = "[^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?]";
        String regxp1 = "(^-?([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$)";
        String regxp2 = "/^[1-9]{1}\\d*(.\\d{1,2})?$|^0.\\d{1,2}$/";

        String regxp3 = "((-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?)";
        String regxp4 = "((-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?|(—))";

        Pattern p = Pattern.compile(regxp4);
        Matcher m = p.matcher(str);
        int count = 0;
        while (m.find()) {
            String pStr = m.group();
            if ("保险费合计".equals(item)) {
                return pStr;
            }
            if (++count == 3) {
                return pStr;
            }
//            System.out.println(pStr);
        }
        return "";
    }

}
